home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / ncurses-5.3 / ncurses / trace / lib_trace.c next >
Encoding:
C/C++ Source or Header  |  2002-10-27  |  5.6 KB  |  181 lines

  1. /****************************************************************************
  2.  * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
  3.  *                                                                          *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a  *
  5.  * copy of this software and associated documentation files (the            *
  6.  * "Software"), to deal in the Software without restriction, including      *
  7.  * without limitation the rights to use, copy, modify, merge, publish,      *
  8.  * distribute, distribute with modifications, sublicense, and/or sell       *
  9.  * copies of the Software, and to permit persons to whom the Software is    *
  10.  * furnished to do so, subject to the following conditions:                 *
  11.  *                                                                          *
  12.  * The above copyright notice and this permission notice shall be included  *
  13.  * in all copies or substantial portions of the Software.                   *
  14.  *                                                                          *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
  16.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
  17.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
  18.  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
  19.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
  20.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
  21.  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
  22.  *                                                                          *
  23.  * Except as contained in this notice, the name(s) of the above copyright   *
  24.  * holders shall not be used in advertising or otherwise to promote the     *
  25.  * sale, use or other dealings in this Software without prior written       *
  26.  * authorization.                                                           *
  27.  ****************************************************************************/
  28.  
  29. /****************************************************************************
  30.  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  31.  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
  32.  ****************************************************************************/
  33.  
  34. /*
  35.  *    lib_trace.c - Tracing/Debugging routines
  36.  */
  37.  
  38. #include <curses.priv.h>
  39. #include <tic.h>
  40.  
  41. #include <ctype.h>
  42.  
  43. MODULE_ID("$Id: lib_trace.c,v 1.50 2002/10/12 15:20:15 tom Exp $")
  44.  
  45. NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0;    /* always define this */
  46.  
  47. #ifdef TRACE
  48. NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
  49. NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
  50.  
  51. static FILE *tracefp;        /* default to writing to stderr */
  52.  
  53. NCURSES_EXPORT(void)
  54. trace(const unsigned int tracelevel GCC_UNUSED)
  55. {
  56.     static bool been_here = FALSE;
  57.     static char my_name[] = "trace";
  58.  
  59.     if (!been_here && tracelevel) {
  60.     been_here = TRUE;
  61.  
  62.     _nc_tracing = tracelevel;
  63.     if (_nc_access(my_name, W_OK) < 0
  64.         || (tracefp = fopen(my_name, "wb")) == 0) {
  65.         perror("curses: Can't open 'trace' file: ");
  66.         exit(EXIT_FAILURE);
  67.     }
  68.     /* Try to set line-buffered mode, or (failing that) unbuffered,
  69.      * so that the trace-output gets flushed automatically at the
  70.      * end of each line.  This is useful in case the program dies. 
  71.      */
  72. #if HAVE_SETVBUF        /* ANSI */
  73.     (void) setvbuf(tracefp, (char *) 0, _IOLBF, 0);
  74. #elif HAVE_SETBUF        /* POSIX */
  75.     (void) setbuffer(tracefp, (char *) 0);
  76. #endif
  77.     _tracef("TRACING NCURSES version %s (tracelevel=%#x)",
  78.         curses_version(), tracelevel);
  79.     } else if (_nc_tracing != tracelevel) {
  80.     _nc_tracing = tracelevel;
  81.     _tracef("tracelevel=%#x", tracelevel);
  82.     }
  83. }
  84.  
  85. NCURSES_EXPORT(void)
  86. _tracef(const char *fmt,...)
  87. {
  88.     static const char Called[] = T_CALLED("");
  89.     static const char Return[] = T_RETURN("");
  90.     static int level;
  91.     va_list ap;
  92.     bool before = FALSE;
  93.     bool after = FALSE;
  94.     int doit = _nc_tracing;
  95.     int save_err = errno;
  96.  
  97.     if (strlen(fmt) >= sizeof(Called) - 1) {
  98.     if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
  99.         before = TRUE;
  100.         level++;
  101.     } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
  102.         after = TRUE;
  103.     }
  104.     if (before || after) {
  105.         if ((level <= 1)
  106.         || (doit & TRACE_ICALLS) != 0)
  107.         doit &= (TRACE_CALLS | TRACE_CCALLS);
  108.         else
  109.         doit = 0;
  110.     }
  111.     }
  112.  
  113.     if (doit != 0) {
  114.     if (tracefp == 0)
  115.         tracefp = stderr;
  116.     if (before || after) {
  117.         int n;
  118.         for (n = 1; n < level; n++)
  119.         fputs("+ ", tracefp);
  120.     }
  121.     va_start(ap, fmt);
  122.     vfprintf(tracefp, fmt, ap);
  123.     fputc('\n', tracefp);
  124.     va_end(ap);
  125.     fflush(tracefp);
  126.     }
  127.  
  128.     if (after && level)
  129.     level--;
  130.     errno = save_err;
  131. }
  132.  
  133. /* Trace 'bool' return-values */
  134. NCURSES_EXPORT(NCURSES_BOOL)
  135. _nc_retrace_bool(NCURSES_BOOL code)
  136. {
  137.     T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
  138.     return code;
  139. }
  140.  
  141. /* Trace 'int' return-values */
  142. NCURSES_EXPORT(int)
  143. _nc_retrace_int(int code)
  144. {
  145.     T((T_RETURN("%d"), code));
  146.     return code;
  147. }
  148.  
  149. /* Trace 'unsigned' return-values */
  150. NCURSES_EXPORT(unsigned)
  151. _nc_retrace_unsigned(unsigned code)
  152. {
  153.     T((T_RETURN("%#x"), code));
  154.     return code;
  155. }
  156.  
  157. /* Trace 'char*' return-values */
  158. NCURSES_EXPORT(char *)
  159. _nc_retrace_ptr(char *code)
  160. {
  161.     T((T_RETURN("%s"), _nc_visbuf(code)));
  162.     return code;
  163. }
  164.  
  165. /* Trace 'SCREEN *' return-values */
  166. NCURSES_EXPORT(SCREEN *)
  167. _nc_retrace_sp(SCREEN * code)
  168. {
  169.     T((T_RETURN("%p"), code));
  170.     return code;
  171. }
  172.  
  173. /* Trace 'WINDOW *' return-values */
  174. NCURSES_EXPORT(WINDOW *)
  175. _nc_retrace_win(WINDOW *code)
  176. {
  177.     T((T_RETURN("%p"), code));
  178.     return code;
  179. }
  180. #endif /* TRACE */
  181.